﻿// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they bagin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
// 
// - Sun Tsu,
// "The Art of War"

using log4net.Core;
using log4net.Util;

namespace Infrastructure.Logging
{
    /// <summary>
    /// The log4net Global Context.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The <c>GlobalContext</c> provides a location for global debugging 
    /// information to be stored.
    /// </para>
    /// <para>
    /// The global context has a properties map and these properties can 
    /// be included in the output of log messages. The <see cref="log4net.Layout.PatternLayout"/>
    /// supports selecting and outputing these properties.
    /// </para>
    /// <para>
    /// By default the <c>log4net:HostName</c> property is set to the name of 
    /// the current machine.
    /// </para>
    /// </remarks>
    /// <example>
    /// <code lang="C#">
    /// GlobalContext.Properties["hostname"] = Environment.MachineName;
    /// </code>
    /// </example>
    /// <threadsafety static="true" instance="true" />
    /// <author>Nicko Cadell</author>
    public static class GlobalContext
    {
        #region Data members

        private static readonly GlobalContextProperties sProp;

        #endregion

        
        /// <summary>
        /// init crap
        /// </summary>
        static GlobalContext()
        {
            sProp = new GlobalContextProperties( log4net.GlobalContext.Properties );
            log4net.GlobalContext.Properties[LoggingEvent.HostNameProperty] = SystemInfo.HostName;
        }

        /// <summary>
        /// The global properties map.
        /// </summary>
        /// <value>
        /// The global properties map.
        /// </value>
        /// <remarks>
        /// <para>
        /// The global properties map.
        /// </para>
        /// </remarks>
        public static GlobalContextProperties Properties
        {
            get { return sProp; }
        }

        /// <summary>
        /// Get\Set the service ID value.
        /// </summary>
        public static int? ServiceId
        {
            get { return (int?)sProp["ServiceID"]; }
            set { sProp["ServiceID"] = value; }
        }

        /// <summary>
        /// Get\Set the component ID value.
        /// </summary>
        public static int? ComponentId
        {
            get { return (int?)sProp["ComponentID"]; }
            set { sProp["ComponentID"] = value; }
        }

        /// <summary>
        /// Get\Set the user name value.
        /// </summary>
        public static string Username
        {
            get { return (string)sProp["UserName"]; }
            set { sProp["UserName"] = value; }
        }

        /// <summary>
        /// Get\Set the session id value.
        /// </summary>
        public static object SessionId
        {
            get { return sProp["SessionID"]; }
            set { sProp["SessionID"] = value; }
        }
    }
}